package com.ajx.blog.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ajx.blog.entity.Menu;
import com.ajx.blog.entity.Roles;
import com.ajx.blog.mapper.MenuMapper;
import com.ajx.blog.service.api.MenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    /**
     * @param {Menu} menu 菜单对象
     * @return {boolean} 是否添加成功 true/false
     * 
     * @description: 添加一个菜单
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean insertOneMenu(Menu menu) {
        if (menu != null) {
            if (menu.getParentId() == -1) {
                menu.setParentId(null);
            }
            return menuMapper.insert(menu) > 0;
        } else {
            return false;
        }
    }

    /**
     * @param {Menu} menu 菜单对象
     * @return {boolean} 是否修改成功 true/false
     * 
     * @description: 修改一个菜单
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean updateOneMenu(Menu menu) {
        if (menu.getParentId() == -1) {
            menu.setParentId(null);
        }
        return menuMapper.updateById(menu) > 0;
    }

    /**
     * @param {Integer} mid 菜单id
     * @return {Menu} 菜单对象
     * 
     * @description: 通过菜单id查询一个菜单
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Menu selectOneMenu(Integer mid) {
        return menuMapper.selectById(mid);
    }

    /**
     * @param {Map<String,Object>} map
     *                             搜索条件封装的map对象,如果parentId为空则所有一级菜单栏,不为空则搜索对应的二级菜单栏。如果角色列表不为空则搜索对应角色的菜单列表,为空则不安角色搜索
     * @return {List<Menu>} 角色对应的菜单栏map集合
     * 
     * @description: 根据角色查询菜单栏
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public List<Menu> selectMenuByMap(Map<String, Object> map) {
        QueryWrapper<Menu> qw = new QueryWrapper<>();
        if (map.get("parentId") == null) {
            qw.isNull("parent_id");
        } else {
            qw.eq("parent_id", map.get("parentId"));
        }
        if (map.get("roleList") != null && ((List) map.get("roleList")).size() > 0) {
            String str = "";
            for (Roles role : (List<Roles>) map.get("roleList")) {
                str += "," + role.getId();
            }
            str = str.substring(1);
            qw.inSql("id", "select menus from roles_menu where roles in (" + str + ")");
        }
        return menuMapper.selectList(qw);
    }

    /**
     * @param {Map<String,Object>} map
     *                             搜索条件封装的map对象,如果parentId为空则所有一级菜单栏,不为空则搜索对应的二级菜单栏
     * @return {List<Menu>} 菜单栏map集合
     * 
     * @description: 查询所有菜单
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public List<Menu> selectAllMenuByMap(Map<String, Object> map) {
        QueryWrapper<Menu> qw = new QueryWrapper<>();
        if (map.get("parentId") == null) {
            qw.isNull("parent_id");
        } else {
            qw.eq("parent_id", map.get("parentId"));
        }
        return menuMapper.selectList(qw);
    }

    /**
     * @param {Integer} page 当前页码数
     * @return {PageInfo<Menu>} 封装的分页信息对象
     * 
     * @description: 查询所有菜单
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public PageInfo<Menu> selectAllMenuByLimit(Integer page) {
        if (page != null) {
            QueryWrapper<Menu> qw = new QueryWrapper<>();
            PageHelper.startPage(page, 5);
            List<Menu> adminList = menuMapper.selectList(qw);
            PageInfo<Menu> info = new PageInfo<Menu>(adminList);
            return info;
        } else {
            return null;
        }
    }

    /**
     * @param {Integer} rid 角色id
     * @return {Map<Integer, Object>} 菜单栏map集合
     * 
     * @description: 根据角色id查询菜单栏
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Map<Integer, Object> selectAdminMenu(Integer rid) {
        Map<Integer, Object> map = new HashMap<>();
        QueryWrapper<Menu> qw = new QueryWrapper<>();
        qw.inSql("id", "select menus from roles_menu where roles = " + rid);
        // 将list集合转为map集合
        for (Menu m : menuMapper.selectList(qw)) {
            map.put(m.getId(), m);
        }
        return map;
    }

}
